<template>
  <template v-if="visible">
    <Teleport to="body">
      <div class="overlay" @click="overlayClose"></div>
      <div class="dialog">
        <header>
          <h3>{{ title }}</h3>
          <span @click="Xclose">X</span>
        </header>
        <main>
          <slot name="content" />
        </main>
        <footer>
          <button @click="completeClose">{{ textComplete }}</button>
          <button @click="cancelClose">关闭</button>
        </footer>
      </div>
    </Teleport>
  </template>
</template>
<script lang="ts">
import { defineComponent, ref } from "vue";

export default defineComponent({
  props: {
    visible: { type: Boolean, default: false },
    title: { type: String, default: "标题" },
    touchOverlayClose: { type: Boolean, default: true },
    confirm: Function,
    cancel: Function,
  },
  setup(props, context) {
    const textComplete = ref("完成");

    const close = () => {
      context.emit("update:visible", !props.visible);
    };

    const overlayClose = () => {
      props.touchOverlayClose ? close() : "";
    };

    const Xclose = () => {
      close();
    };

    const cancelClose = () => {
      props.cancel?.() ? props.cancel() : close();
    };

    const completeClose = () => {
      props.confirm?.();
    };

    return { overlayClose, Xclose, cancelClose, completeClose, textComplete };
  },
});
</script>
<style scoped lang="scss">
.overlay {
  height: 100vh;
  width: 100vw;
  position: absolute;
  top: 0;
  left: 0;
  background: black;
  opacity: 0.6;
}
.dialog {
  border-radius: 8px;
  width: 30%;
  z-index: 1;
  position: absolute;
  background: white;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 30px;
    border-bottom: 1px solid grey;
    padding: 10px;
    span {
      cursor: pointer;
    }
  }
  main {
    padding: 0 10px;
  }
  footer {
    border-top: 1px solid grey;
    padding: 10px;
    text-align: end;
    button {
      margin-left: 12px;
    }
  }
}
</style>
